package cn.myit.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.myit.domain.Login;
import cn.myit.excption.myexcption;
import cn.myit.mapper.LoginMapper;
import cn.myit.service.ILoginService;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class LoginServiceImpl implements ILoginService {

    ConcurrentHashMap<String,Login> map = new ConcurrentHashMap();

    public ConcurrentHashMap<String, Login> getMap() {
        return map;
    }

    public void setMap(ConcurrentHashMap<String, Login> map) {
        this.map = map;
    }

    @Autowired
    private LoginMapper loginMapper;
    @Override
    public String login(Login login) {

        String username = login.getUsername();
        String password = login.getPassword();
        //校验用户名密码不能为空
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new myexcption("用户名密码为空");
        }
        //根据用户名查询
        //查询不到
        Login tmp = loginMapper.selectByname(username);
        if (ObjectUtil.isEmpty(tmp)){
            throw new myexcption("用户名错误");
        }
        //查询到了，判断密码
        if(!password.equals(tmp.getPassword())){
            throw new myexcption("密码错误");
        }
        UUID uuid = UUID.randomUUID();
        map.put(uuid.toString(),login);
        return uuid.toString();
    }
}
